package com.qiniu.githubdata.ip;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class demo {
    final static String secretId = "ozfhzt26h5xb30xo53zu";
    final static String signature = "jzkgoem6d6n1wforv2yxrdcbj9dcz3nw";
    final static String urlString = "https://dps.kdlapi.com/api/getdps/?secret_id=" + secretId + "&signature=" + signature + "&num=1&pt=1&format=text&sep=1,";

    // 用户名密码认证(私密代理/独享代理)
    final static String ProxyUser = "d1112721220";
    final static String ProxyPass = "gxj3gnzm";

    public static String getUrlProxyContent(String url, String ip, int port) {
        Authenticator.setDefault(new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray());
            }
        });

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));

        try {
            // 此处自己处理异常、其他参数等
            Document doc = Jsoup.connect(url)
                    .followRedirects(false).timeout(5000)
                    .proxy(proxy).get();
            if (doc != null) {
                System.out.println(doc.body().html());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) throws Exception {
        // 目标网站
        String targetUrl = "https://github.com/";

        // JDK 8u111版本后，目标页面为HTTPS协议，启用proxy用户密码鉴权
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
        try {
            URL _url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) _url.openConnection();
            connection.setRequestMethod("GET");

            // 读取响应
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                String[] parts = response.toString().split(":");
                if (parts.length >= 2) {
                    String ip = parts[0].trim();
                    int port = Integer.parseInt(parts[1].trim());
                    System.out.println("IP: " + ip);
                    System.out.println("Port: " + port);
                    getUrlProxyContent(targetUrl, ip, port);
                }
            } else {
                System.out.println("GET request not worked");
            }
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}